/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */

package core;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import model.Decision;
import model.Document;
import model.Docversion;
import model.Employee;
import org.hibernate.Query;
import org.hibernate.Session;
import org.hibernate.Transaction;
import org.omg.CORBA.TRANSACTION_MODE;
import util.ProjetHibernateUtil;

/**
 *
 * @author phinguyen
 */
public class telechargertest {
    private static Session session;
    
    public telechargertest(){
        this.session= ProjetHibernateUtil.getSessionFactory().openSession();
    }
    
    public List<Docversion> getallDocVersion(){
        List alldoc= new ArrayList<Docversion>();
        alldoc=this.session.getNamedQuery("HQL_ALL_DOCVERSION").list();
        return alldoc;
    }
    public String getURL(Integer id){
        return (String)this.session.getNamedQuery("HQL_DOCVERSION_ID").setParameter("id", id).uniqueResult();
        
    }
    public String getTITLE(Integer id){
        return (String)this.session.getNamedQuery("HQL_DOCVERSION_TITLE").setParameter("id", id).uniqueResult();
        
    }
    
    public void saveDocVersion(Integer docid,Integer eid,Integer decision,String title, Date date, String url){
        Document doc= new Document();
        doc=(Document)session.getNamedQuery("HQL_DOC_ID").setParameter("iddoc", docid).uniqueResult();
        System.out.println(doc);
        Employee emp=new Employee();
        emp=(Employee)session.getNamedQuery("HQL_EMPLOYEE_ID").setParameter("id", eid).uniqueResult();
        System.out.println(emp);
        Decision dec=new Decision();
        dec=(Decision)session.getNamedQuery("HQL_ALL_DECISION_ID").setParameter("decisionid", decision).uniqueResult();
        Docversion docversion=new Docversion(doc, emp,dec, title,date, url);
        Transaction tr = session.getTransaction();
        tr.begin();
        session.saveOrUpdate(docversion);
        tr.commit();
    }
    public List<Document> getAllDocument(){
        return session.getNamedQuery("HQL_DOC").list();
    }
    
     public List<Employee> getAllEmployees() {
        Query query = session.getNamedQuery("HQL_ALL_EMPLOYEE");
        List result = query.list();
        return result;
    }
    
     public List<Decision> getAllDecision(){
         return session.getNamedQuery("HQL_ALL_DECISION").list();
     }
    public Decision getDocversionID(Integer iddocv){
        return(Decision)session.getNamedQuery("HQL_ALL_DOCVERSION_OBJET").setParameter("id", this).uniqueResult();
    }
    
        public static void main(String[] args){
        telechargertest tcore=new telechargertest();
        boolean s= false;
        
//        tcore.saveDocVersion(3, 6,4,"ffffffffff", new Date(), "urll");
//        System.out.println(tcore.getURL(1));
        System.out.println(tcore.getallDocVersion().size());
    }
}
